# Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None
class Solution: # @param head, a list node # @return a tree node def convert(self,head,tail): if head==tail: return None if head.next==tail: return TreeNode(head.val) mid=head fast=head while fast!=tail and fast.next!=tail: fast=fast.next.next mid=mid.next node=TreeNode(mid.val) node.left=self.convert(head,mid) node.right=self.convert(mid.next,tail) return node def sortedListToBST(self, head): return self.convert(head,None)